home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / lib / flyEngine / flyParticle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-29  |  1.4 KB  |  89 lines

  1. #include "../Fly3D.h"
  2.  
  3. int particle::compute_collision(vector& p,vector& v)
  4. {
  5.     static vector p0,dir,d,v1,v2,reflectdir,*n;
  6.  
  7.     float f1,f2,len;
  8.     int ncol=-1;
  9.     bsp_object *last_hit_obj=0;
  10.     p0=pos;
  11.     flyengine->moving=1;
  12.     while(ncol<2)
  13.     {
  14.         dir=p-p0;
  15.         len=dir.length();
  16.         if (len<0.1f)
  17.             break;
  18.         if (ncol==-1)
  19.             ncol++;
  20.         dir/=len;
  21.  
  22.         if (0==flyengine->collision_bsp(flyengine->bsp,p0,p,0,radius))
  23.         {
  24.             p0=p - dir*0.1f;
  25.             break;
  26.         }
  27.         
  28.         ncol++;
  29.  
  30.         last_hit_obj=flyengine->hitobj;
  31.         n=&flyengine->hitmesh->faces[flyengine->hitface]->normal;
  32.         p0=flyengine->hitip - dir*0.1f;
  33.  
  34.         reflectdir=dir + (*n)*(-2.0f*vec_dot(*n,dir));
  35.         reflectdir.normalize();
  36.         f1=vec_dot(*n,reflectdir);
  37.  
  38.         f2=(p-p0).length();
  39.         d=reflectdir*f2;
  40.         v1=(*n)*(f1*f2);
  41.         v2=d-v1;
  42.         p=p0+v1*bump+v2*friction;
  43.  
  44.         f2=v.length();
  45.         d=reflectdir*f2;
  46.         v1=(*n)*(f1*f2);
  47.         v2=d-v1;
  48.         v=v1*bump+v2*friction;
  49.     }
  50.     p=p0;
  51.     flyengine->moving=0;
  52.     flyengine->hitobj=last_hit_obj;
  53.     return ncol;
  54. }
  55.  
  56. int particle::step(int dt)
  57. {
  58.     static vector p,v;
  59.     float dtf=(float)dt;
  60.     int move=1;
  61.     
  62.     p.x=pos.x+dtf*vel.x;
  63.     p.y=pos.y+dtf*vel.y;
  64.     p.z=pos.z+dtf*vel.z;
  65.     v.x=vel.x+dtf*(force.x/mass);
  66.     v.y=vel.y+dtf*(force.y/mass);
  67.     v.z=vel.z+dtf*(force.z/mass);
  68.     life-=dt;
  69.  
  70.     if (col_flag&1)
  71.     {
  72.         move=compute_collision(p,v);
  73.         if (move==-1)
  74.             move=0;
  75.         else
  76.         {
  77.             if (move&&(col_flag&2))
  78.                 life=-1;
  79.             move=1;
  80.         }
  81.     }
  82.  
  83.     pos=p;
  84.     vel=v;
  85.  
  86.     return move;
  87. }
  88.  
  89.